package com.wang.exam.controller;

import com.wang.exam.pojo.Grade;
import com.wang.exam.pojo.JavaWebExam;
import com.wang.exam.pojo.StudentExamCourse;
import com.wang.exam.service.StudentExamCourseService;
import com.wang.exam.service.StudentGradeService;
import com.wang.exam.service.TestQuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ExamMessageController {
    private ArrayList<ArrayList<String>> answer;
    private int grade;
    private Integer course_id;
    private List<JavaWebExam> list;
    private String test_name;
    @Autowired
    StudentExamCourseService studentExamCourseService;
    @Autowired
    StudentGradeService studentGradeService;
    @Autowired
    TestQuestionsService testQuestionsService;
    @RequestMapping("/studentCourseList")
    @ResponseBody
    public Object studentExamCourse(HttpSession httpSession){
        Integer student_id = (Integer) httpSession.getAttribute("student_id");
        List<StudentExamCourse> list = studentExamCourseService.getCourses(student_id);
        return list;
    }
    @RequestMapping("/student/startExam")
    public String studentStartExam(@RequestParam("course_id") Integer course_id,Model model,HttpSession httpSession) {
        this.course_id = course_id;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("major_id", (Integer) httpSession.getAttribute("major_id"));
        map.put("student_id", (Integer) httpSession.getAttribute("student_id"));
        map.put("course_id", course_id);
        List<Grade> listGrades = studentGradeService.getGrades(map);
        List<StudentExamCourse> listCourse = studentExamCourseService.getCourse((Integer) httpSession.getAttribute("student_id"),course_id);
        try {
            test_name = testQuestionsService.getTestName(course_id).get(0).getTest_name();
        }catch (Exception e){
            return "Error2";
        }
        List<JavaWebExam> lists = testQuestionsService.getAllQuestion(test_name);
        int a = listGrades.size();
        if(listCourse.size()==1 && a==0){             //没有考过试
            model.addAttribute("lists",lists);
            return "exam_856701";
        }else if(a==1){                              //已经考过试
           return "Error" ;
        }else {
            return "Error2";                               //输入错误
        }

    }

    @RequestMapping("/examGrade")
    @ResponseBody
    public String getEXamGrade(@RequestBody Map<Integer,Object> map,HttpSession httpSession){
        List<JavaWebExam> list = studentExamCourseService.getTexts();
        for (int i = 0;i < list.size();i++){
            if(i<20){
                if(list.get(i).getAnswer().equals(map.get(i))){
                    grade += 3;
                }
            }else {
                ArrayList<String> arrayList = (ArrayList<String>) map.get(i);
                String[] arrayList1 = list.get(i).getAnswer().split(",");
                int m = arrayList.size();
                int n = arrayList1.length;
                int l = Math.min(n,m);
                for(int j = 0;j<l;j++){
                    if(arrayList.get(j).equals(arrayList1[j])){
                        grade+=4;
                    }
                }
            }

        }
        Map<String,Object> m = new HashMap<String,Object>();
        m.put("major_id",(Integer) httpSession.getAttribute("major_id"));
        m.put("student_id",(Integer) httpSession.getAttribute("student_id"));
        m.put("course_id",course_id);
        m.put("grade",grade);
        int n = studentGradeService.AddGrade(m);

       return "123";
    }
}
